﻿using System;
using System.Collections.Specialized;
using System.Configuration;
using System.Web.Configuration;
using System.Web.Hosting;

namespace MvcCms.Service.Code.Membership
{
    public static class CacheConfig
    {

        private static string GetConfigValue(string configValue, string defaultValue)
        {
            return String.IsNullOrEmpty(configValue) ? defaultValue : configValue;
        }
        public static NameValueCollection SetMemberShipConfig()
        {
            MembershipSection membershipSection = (MembershipSection)WebConfigurationManager.GetSection("system.web/membership");
            string defaultProvider = membershipSection.DefaultProvider;
            ProviderSettings providerSettings = membershipSection.Providers[defaultProvider];
            NameValueCollection config = providerSettings.Parameters;

            if (String.IsNullOrEmpty(config["description"]))
            {
                config.Remove("description");
                config.Add("description", "MvcCms Membership Provider");
            }

            config["applicationName"] = GetConfigValue(config["applicationName"], HostingEnvironment.ApplicationVirtualPath);
            config["maxInvalidPasswordAttempts"] = GetConfigValue(config["maxInvalidPasswordAttempts"], "5");
            config["passwordAttemptWindow"] = GetConfigValue(config["passwordAttemptWindow"], "10");
            config["minRequiredNonAlphanumericCharacters"] = GetConfigValue(config["minRequiredNonAlphanumericCharacters"], "1");
            config["minRequiredPasswordLength"] = GetConfigValue(config["minRequiredPasswordLength"], "7");
            config["passwordStrengthRegularExpression"] = GetConfigValue(config["passwordStrengthRegularExpression"], "");
            config["enablePasswordReset"] = GetConfigValue(config["enablePasswordReset"], "false");
            config["enablePasswordRetrieval"] = GetConfigValue(config["enablePasswordRetrieval"], "false");
            config["requiresQuestionAndAnswer"] = GetConfigValue(config["requiresQuestionAndAnswer"], "false");
            config["requiresUniqueEmail"] = GetConfigValue(config["requiresUniqueEmail"], "true");

            config["passwordFormat"] = config["passwordFormat"] ?? "Hashed";
            config["validationKey"] = ConfigurationManager.AppSettings["MembershipHashKey"];

            return config;
        }
    }
}
